// basic/components/gcs-component/news/news-comment.js
// 获取应用实例
const app = getApp()
Component({
    options: {
        addGlobalClass: true
    },
    /**
     * 组件的属性列表
     */
    properties: {
        listData: {
            type: Object,
            default: []
        },
        bCommity: { //是否开启评论 默认关闭
            type: Boolean,
            default: false
        },
        pagepoint: {
            type: String,
            default: ""
        },
        iNewsID: {
            type: String,
            default: ''
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        listData2: [],
        listDataNew: [],
        ibHeight: 0, //inputbox height
        bInput: false, //是否输入
        bFocus: false,
        wordsNum: 0,
        commentValue: ""
    },
    observers: {
        'listData': function (listData) {
            this.formatData(listData);
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {
        formatData: function (listData) {
            //数据显示处理
            for (var i = 0; i < listData.length; i++) {
                listData[i]['sTime'] = wx.$to_encodeTimeWords(listData[i]['iTime'], "Y-M-D h:m:s");
                listData[i]['sNickName'] = wx.$to_decodeBase64(listData[i]['sNickName']);
                listData[i]['sContent'] = wx.$to_decodeBase64(listData[i]['sContent']);
            }
            this.setData({
                listData2: listData
            })
        },
        inputFocus(e) {
            this.setData({
                ibHeight: e.detail.height,
                bFocus: true
            })
        },
        inputBlur() {
            this.setData({
                bFocus: false
            })
        },
        showModal() {
            this.setData({
                bInput: !this.data.bInput,
                bFocus: true,
                wordsNum: 0,
                commentValue: ""
            })
        },
        hideModal() {
            this.setData({
                bInput: !this.data.bInput
            })
        },
        commentInput(e) {
            var commentValue = e.detail.value;
            this.setData({
                commentValue: commentValue,
                wordsNum: commentValue.length
            })
        },
        sendComment() {
            var _this = this;
            if (_this.data.commentValue != "") {
                var pagepoint = _this.data.pagepoint;
                var api_common_news = app.globalData.api_common_news;
                if (api_common_news.hasOwnProperty(pagepoint)) {
                    //检测是否存在评论接口
                    if (api_common_news[pagepoint]['sCommityApi'] != "" && api_common_news[pagepoint]['sCommityApi'] != undefined) {
                        var commityObj = {};
                        commityObj['sContent'] = wx.$to_encodeBase64(_this.data.commentValue);
                        commityObj['sOpenID'] = "123123123123123";
                        if (api_common_news[pagepoint]['sCommityPost'] != "") {
                            commityObj[api_common_news[pagepoint]['sCommityPost']] = _this.data.iNewsID;
                        } else {
                            commityObj['iID'] = _this.data.iNewsID;
                        }
                        wx.$requestApi(api_common_news[pagepoint]['sCommityApi'], commityObj, function (res) {
                            if (res.code == 0) { //更新页面数据
                                _this.setData({
                                    bInput: false
                                })
                                wx.pageScrollTo({
                                    scrollTop: 0
                                })
                                wx.$gcs_showToast('评论成功', 'success');

                                var listDataNew = _this.data.listDataNew;
                                listDataNew.unshift({
                                    iID: res.data.iID,
                                    sNickName: wx.$to_decodeBase64(res.data.sNickName),
                                    sContent: wx.$to_decodeBase64(res.data.sContent),
                                    sTime: wx.$to_encodeTimeWords(res.data.iTime, "Y-M-D h:m:s"),
                                    sPic: res.data.sPic
                                });
                                _this.setData({
                                    listDataNew: listDataNew
                                })
                            } else {
                                wx.$gcs_showToast(res.msg);
                            }
                        })
                    } else {
                        wx.$gcs_showToast("缺少评论接口");
                    }
                } else {
                    wx.$gcs_showToast("不存在的页面标识");
                }
            } else {
                wx.$gcs_showToast("输入内容不能为空");
            }
        }
    }
})